﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Model;

namespace Common
{
   public class Tools
    {
       public StockVirtual GetByCode(String code)
       {
           return new StockVirtual();
       }
       public List<StockVirtual> GetAll()
       {
           List<StockVirtual> stockList = new List<StockVirtual>();
           String str = GetUrl();
           Uri uri = new Uri(str);
           System.Net.WebRequest req = System.Net.WebRequest.Create(uri);
           System.Net.WebResponse resp = req.GetResponse();
           System.IO.Stream str3 = resp.GetResponseStream();
           System.IO.StreamReader reader = new System.IO.StreamReader(str3, System.Text.Encoding.Default);
           String s = reader.ReadToEnd().Trim();
           String kon = ";";
           String[] stockString = s.Split(kon.ToCharArray());
           int n1 = stockString.Length - 1;
           List<StockVirtual> StockList = new List<StockVirtual>();
           for (int i = 0; i < stockString.Length - 1; i++)
           {
               String rep = "var hq_str_s_"; //"var hq_str_";
               String tri = "\"";
               stockString[i] = stockString[i].Trim().Replace(rep, "");
               stockString[i] = stockString[i].Trim().Replace(tri, "");
               stockString[i] = stockString[i].Replace("=", ",");
              String[] tempString = stockString[i].Split(',');
              StockVirtual StockModel = new StockVirtual();
              StockModel.StockCode = tempString[0].Substring(2);
              StockModel.StockName = tempString[1];
              StockModel.XianJia = decimal.Parse(tempString[2]);
              StockModel.UpDownMargin = decimal.Parse(tempString[3]);
              StockModel.UpDownCount = decimal.Parse(tempString[4]);
              StockModel.DealMoney = decimal.Parse(tempString[5]);
              StockModel.DealCount = decimal.Parse(tempString[6]);
              stockList.Add(StockModel);
           }
           reader.Close();
           return stockList;
       }

       private string GetUrl()
       {
           GetCode();
           String back="http://hq.sinajs.cn/list=s_sz000630,s_sh600756,s_sz000809,s_sz002431,s_sh600869,s_sz002006,s_sh600735,s_sh600851,s_sz002300,s_sz002371,s_sz300092,s_sz000929,s_sh601006";
           return back;
       }

       private Stock GetCode()
       {
           return new Stock();
       }
    }
}
           